package cn.ticket.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Constants;
import cn.ticket.model.Feedback;
import cn.ticket.service.FeedbackService;
import cn.ticket.service.NewsSetService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("setting")
public class SettingController {
	@Autowired
	FeedbackService feedbackService;
	@Autowired
	NewsSetService newsSetService;
@RequestMapping(value="about",produces={"application/json;charset=utf-8"})
@ResponseBody
public String about(){
	
	Map<String, Object> result=new HashMap<>();
	result.put("status", 1001);
	result.put("body", "任何使用问题请登录bbs.npic.site进行查看,还可以进行讨论留言哦...此消息来自服务器,服务器时间戳: "+new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
	return Utility.gson.toJson(result);
	}
@RequestMapping(value="help",produces={"application/json;charset=utf-8"})
@ResponseBody
public String help(){
	String content=newsSetService.getByName("使用帮助").get(0).getContent();
	Map<String, Object> result=new HashMap<>();
	result.put("status", 1001);
	result.put("body", content);
	return Utility.gson.toJson(result);
	}
@RequestMapping(value="softRule",produces={"application/json;charset=utf-8"})
@ResponseBody
public String softRule(){
	String content=newsSetService.getByName("软件规则").get(0).getContent();
	Map<String, Object> result=new HashMap<>();
	result.put("status", 1001);
	result.put("body",content);
	return Utility.gson.toJson(result);
	}
@RequestMapping(value="busRule",produces={"application/json;charset=utf-8"})
@ResponseBody
public String busRule(){
	String content=newsSetService.getByName("所办规则").get(0).getContent();
	Map<String, Object> result=new HashMap<>();
	result.put("status", 1001);
	result.put("body", content);
	return Utility.gson.toJson(result);
	}
@RequestMapping(value="version",produces={"application/json;charset=utf-8"})
@ResponseBody
public String version(){
	Map<String, Object> result=new HashMap<>();
	result.put("status", 1001);
	result.put("body", Constants.LatestVersion);
	return Utility.gson.toJson(result);
	}
@RequestMapping(value="versionUrl",produces={"application/json;charset=utf-8"})
@ResponseBody
public String versionUrl(){
	Map<String, Object> result=new HashMap<>();
	Map<String, Object> data=new HashMap<>();
	data.put("version", Constants.LatestVersion);
	data.put("url", "http://www.npic.site/download/android");
	result.put("status", 1001);
	result.put("body", data);
	return Utility.gson.toJson(result);
	}
@RequestMapping(value="feedback",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String feedback(@RequestBody Map<String, Object> args){
	Integer staffId=(Integer) args.get("staffId");
	String  message=(String) args.get("message");
	Map<String, Object> result=new HashMap<>();
	Feedback record=new Feedback();
	record.setStaffid(staffId);
	record.setMessage(message);
	int num=feedbackService.insertSelective(record);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
	}
}
